home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4868 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  100 lines

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Newbie: How to return Multi-Dimensional Array's?
  5. Date: 7 Feb 96 14:53:14 GMT
  6. Organization: Clemson University
  7. Message-ID: <mjs.823704794@hubcap>
  8. References: <4f7t4m$4bk@news.xs4all.nl> <3118B3F7.2781E494@nada.kth.se>
  9. NNTP-Posting-Host: hubcap.clemson.edu
  10.  
  11. Fredrik Rubensson <prgp-fru@nada.kth.se> writes:
  12.  
  13. >Anthony Moendir wrote:
  14. >> 
  15. >>  How can you return Multi-Dimensional Array's from a function?
  16.  
  17. You can't return an array of any sort from a function (except as a
  18. field in a struct).  But you can return a pointer to an array (see
  19. below).
  20.  
  21. >> I would like to do  something like this:
  22. >> 
  23. >> void main()
  24.    ^^^^
  25. The keyword that belongs here is spelled "int".
  26.  
  27. >> {
  28. >>  char arr[10][5];
  29. >>  tmp=foo();
  30.  
  31.     return 0;
  32.  
  33. >> }
  34.  
  35. >I don't understand the declaration of arr in main. If you want foo and
  36. >main to know about arr you shall declare it before main.
  37.  
  38. But he may not want that.  Clearly this isn't a complete code fragment
  39. (for example, he needs to declare tmp, and arr and tmp are never
  40. used).  As long as he understands that the declaration in foo() is of an
  41. object different from the one in main(), he's OK here.
  42.  
  43. >> char *foo()
  44.  
  45. You need to declare foo before you use it in main.  As it is written,
  46. the compiler will assume that foo() returns an int, which is wrong.
  47.  
  48. >> {
  49. >>  char arr[10][5];
  50.  
  51. You should never return the address of an automatic variable.  Either 
  52. move the declaration of arr outside the function (which makes it global)
  53. or declare it static (which keeps its scope inside foo(), but makes it
  54. outlive the execution of foo()).
  55.  
  56. >> // do something
  57.  
  58. C comments are delimited by /* ... */
  59.  
  60. >> return (arr)
  61. >> }
  62.  
  63. >A multidimensional char-array is represented as an array of
  64. >char-pointers so the return type of foo should be char**. 
  65.  
  66. No, a multidimensional char array is an array of char *arrays*, so
  67. the return type of foo() (and incidentally the type of tmp in main())
  68. should be char (*)[5] (pointer to array of 5 chars).
  69.  
  70.     char (*foo())[5];  /* function returning pointer to array of 5 chars */
  71.  
  72.     int main()
  73.     {
  74.        char arr[10][5];
  75.        char (*tmp)[5] = foo(); /* pointer to array of 5 chars */
  76.  
  77.        /* do something, presumably */
  78.  
  79.        return 0;
  80.     }
  81.  
  82.     char (*foo())[5]
  83.     {
  84.        static char arr[10][5];  /* static so address can be used outside */
  85.  
  86.        /* do something */
  87.  
  88.        return arr;    /* arr decays to a pointer to its first element 
  89.                        (an array of 5 chars) */
  90.     }
  91.  
  92. >                                                           The
  93. >parenthesis around arr after return are not needed.
  94.  
  95. But a semicolon *is* needed...
  96. -- 
  97.         Matthew Saltzman
  98.         Clemson University Math Sciences
  99.         mjs@clemson.edu
  100.